package com.github.livebase.util.html;

import android.text.TextUtils;
import android.util.ArrayMap;

import com.github.livebase.util.KeyValuePairs;
import com.github.livebase.util.StringKeyValuePairs;
import com.github.livebase.util.base.ArraysUtil;
import com.github.livebase.util.base.CollectionsUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Element {
        private String name;
        private List<Element> children;
        private Map<String, String> attr;

        private Map<String, String> style;

        public Element(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
        public Element getFirstChildElementByTagName(String tagName) {
            return CollectionsUtil.first(children, (c)-> tagName.equals(c.name) );
        }

        public String getStyle(String name) {
            if (this.style != null) {
                return this.style.get(name);
            }
            String style = getAttribute("style");
            this.style = new ArrayMap<>(0);
            if (TextUtils.isEmpty(style)) {
                return null;
            }
            String[] kvps = style.split(";");
            ArraysUtil.foreach(kvps, (i, s)-> {
                StringKeyValuePairs kvp = StringKeyValuePairs.createKeyValuePairs(s.trim(), ":");
                if (!TextUtils.isEmpty(kvp.getKey()) && !TextUtils.isEmpty(kvp.getValue())) {
                    this.style.put(kvp.getKey().trim(), kvp.getValue().trim());
                }
            });
            return this.style.get(name);
        }

        public void appendChild(Element element) {
            if (children == null) {
                children = new ArrayList<>();
            }
            children.add(element);
        }
        public boolean containsAttribute(String key) {
            if (attr == null) {
                return false;
            }
            return attr.containsKey(key);
        }
        public String getAttribute(String key) {
            if (!containsAttribute(key)) {
                return null;
            }
            return attr.get(key);
        }
        public void setAttribute(String key, String value) {
            if (attr == null) {
                attr = new HashMap<>();
            }
            attr.put(key, value);
        }

    public List<Element> getChildren() {
        return children;
    }

    public String html() {
            StringBuilder builder = new StringBuilder();
            CollectionsUtil.foreach(children, (i, c) -> {
                builder.append(c.htmlWithTag());
            });
            return builder.toString();
        }

        protected String htmlWithTag() {
            StringBuilder builder = new StringBuilder();
            builder.append('<');
            builder.append(name);

            if (attr != null) {
                CollectionsUtil.foreach(attr.entrySet(), (i ,l) -> {
                    builder.append(String.format(" %s=\"%s\"", l.getKey(), l.getValue()));
                });
            }

            builder.append('>');

            CollectionsUtil.foreach(children, (i, c) -> {
                builder.append(c.htmlWithTag());
            });
            builder.append("</");
            builder.append(name);
            builder.append('>');
            return builder.toString();
        }
        public String text() {
            StringBuilder builder = new StringBuilder();
            CollectionsUtil.foreach(children, (i, c) -> {
                builder.append(c.text());
            });
            return builder.toString();
        }
    }